<template>
  <div class="login-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="@/assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 登录的表单 -->
      <!-- 校验规则生效
      1.form 必须关联 :model="form 对象"
      2.input 双向绑定
      3.form 定义 :rules="reles 对象"
      4.input 所在的form-item 绑定 prop 属性 -->
      <el-form  ref="addFormRef" :model="loginForm" :rules="loginRules">
        <el-form-item prop="username" >
          <el-input
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-button
          type="primary"
          class="btn-login"
          @click="login"
        >登录</el-button>
        <el-link @click="$router.push('/reg')" type="info" class="link-reg">去注册账号</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>
import { reqLogin } from '@/api/user'
export default {
  name: 'Login',
  data () {
    return {
      // 登录关联的数据
      loginForm: {
        username: 'xiaoxiao',
        password: 'xiaoxiao'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['change', 'blur'] },
          { pattern: /^[0-9a-zA-Z]{5,10}$/, message: '用户名必须是5-10位的字母数字', trigger: [['change', 'blur']] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['change', 'blur'] },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符串', trigger: [['change', 'blur']] }
        ]
      }
    }
  },
  methods: {
    login () {
    // 校验通过后,再发出请求
      this.$refs.addFormRef.validate(async boo => {
        if (!boo) return null
        // 校验通过后,发送请求
        const { data } = await reqLogin(this.loginForm)
        if (data.code !== 0) {
          return this.$message.error('登陆失败')
        }
        // 登录成功之后
        this.$message.success('登陆成功')
        // 存储 token 到本地
        localStorage.setItem('token', data.token)
        // 跳转到首页
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
    background-color: blue;
  height: 100%;
    background: url("~@/assets/images/login_bg.jpg") no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    height: 270px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
        background: url("~@/assets/images/login_title.png") no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      font-size: 12px;
    }
  }
}
</style>
